package com.pureut.system.service.impl;

import com.pureut.common.security.utils.DictUtils;
import com.pureut.common.security.utils.SecurityUtils;
import com.pureut.system.api.domain.SysDictData;
import com.pureut.system.domain.SysWorkbenchMessage;
import com.pureut.system.domain.dto.SysWorkbenchMessageDto;
import com.pureut.system.domain.vo.SysWorkbenchMessageVo;
import com.pureut.system.mapper.SysWorkbenchMessageMapper;
import com.pureut.system.service.SysWorkbenchMessageService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 消息提醒实现层
 */
@Service
public class SysWorkbenchMessageServiceImpl implements SysWorkbenchMessageService {

    /**
     * 消息提醒mapper
     */
    @Resource
    SysWorkbenchMessageMapper sysWorkbenchMessageMapper;

    /**
     * 消息提醒列表
     *
     * @param sysWorkbenchMessageVo
     * @return
     */
    @Override
    public List<SysWorkbenchMessageDto> getList(SysWorkbenchMessageVo sysWorkbenchMessageVo) {
        sysWorkbenchMessageVo.setUserId(SecurityUtils.getUserId());
        //获取消息提醒类型
        List<SysDictData> array = DictUtils.getDictCache("message_type");
        Map<String, String> messageMap = array.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));

        List<SysWorkbenchMessageDto> messageList = sysWorkbenchMessageMapper.getMessageList(sysWorkbenchMessageVo);
        for (SysWorkbenchMessageDto entity : messageList) {
            //类型
            entity.setMessageTypeDict(messageMap.get(entity.getMessageType()));
        }
        return messageList;
    }

    /**
     * 修改
     *
     * @param sysWorkbenchMessageVo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateStatus(SysWorkbenchMessageVo sysWorkbenchMessageVo) {
        if (sysWorkbenchMessageVo.getId() != null) {
            //根据id修改单个数据
            return sysWorkbenchMessageMapper.updateStatus(sysWorkbenchMessageVo.getId()) > 0;
        } else {
            //获取全部列表
            List<SysWorkbenchMessageDto> messageList = sysWorkbenchMessageMapper.getMessageList(sysWorkbenchMessageVo);
            List<String> idList = new ArrayList<>();
            for (SysWorkbenchMessageDto entity : messageList) {
                idList.add(String.valueOf(entity.getId()));
            }
            //修改全部
            return sysWorkbenchMessageMapper.updateStatusByIds(idList) > 0;
        }
    }

    /**
     * 新增消息提醒
     *
     * @param list
     * @return
     */
    @Override
    public boolean insertWork(List<SysWorkbenchMessageVo> list) {

        return sysWorkbenchMessageMapper.insertWork(list);
    }

    /**
     * 查询对应消息类别的列表
     *
     * @param type
     * @return
     */
    @Override
    public List<SysWorkbenchMessage> getInfo(String type) {
        return sysWorkbenchMessageMapper.getInfo(type);
    }

    /**
     * 查询未读消息数量
     *
     * @param
     * @return
     */
    @Override
    public int getNoMessage() {
        SysWorkbenchMessageVo sysWorkbenchMessageVo = new SysWorkbenchMessageVo();
        Long userId = SecurityUtils.getUserId();
        sysWorkbenchMessageVo.setReadMark(2);
        sysWorkbenchMessageVo.setUserId(userId);
        return sysWorkbenchMessageMapper.getMessageList(sysWorkbenchMessageVo).size();
    }


}
